package flc.ast.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.ArrayUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.view.WheelView;
import com.stark.beat.lib.core.BeatSoundType;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.databinding.DialogTimbreBinding;
import stark.common.basic.base.BaseSmartDialog;

public class TimbreDialog extends BaseSmartDialog<DialogTimbreBinding> implements View.OnClickListener {

    private BeatSoundType currentBeatSoundType;
    private List<BeatSoundType> beatSoundTypes;
    private List<String> timbreStrList;

    public TimbreDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    public void setTimbre(BeatSoundType beatSoundType) {
        currentBeatSoundType = beatSoundType;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_timbre;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivTimbreCancel.setOnClickListener(this);
        mDataBinding.ivTimbreConfirm.setOnClickListener(this);

        int tmpPos = 0;
        timbreStrList=new ArrayList<>();
        beatSoundTypes = ArrayUtils.asList(BeatSoundType.values());
        for (int i = 0; i < beatSoundTypes.size(); i++) {
            if (beatSoundTypes.get(i) == currentBeatSoundType) {
                tmpPos = i;
            }
            timbreStrList.add(beatSoundTypes.get(i).getName());
        }
        WheelAdapter adapter = new ArrayWheelAdapter(timbreStrList);
        mDataBinding.wvTimbre.setAdapter(adapter);

        mDataBinding.wvTimbre.setTextColorCenter(Color.parseColor("#71FFCB"));
        mDataBinding.wvTimbre.setTextSize(21);
        mDataBinding.wvTimbre.setMinimumHeight(200);
        mDataBinding.wvTimbre.setDividerColor(Color.parseColor("#80282A30"));
        mDataBinding.wvTimbre.setDividerType(WheelView.DividerType.FILL);
        mDataBinding.wvTimbre.setItemsVisibleCount(3);
        mDataBinding.wvTimbre.setCurrentItem(tmpPos);
        mDataBinding.wvTimbre.setCyclic(false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivTimbreCancel:
                dismiss();
                break;
            case R.id.ivTimbreConfirm:
                dismiss();
                if (listener != null) {
                    listener.onClickConfirm(beatSoundTypes.get(mDataBinding.wvTimbre.getCurrentItem()));
                }
                break;
        }
    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }

    public interface IListener {
        void onClickConfirm(BeatSoundType beatSoundType);
    }
}
